      <div class="templatemo-content-wrapper">
        <div class="templatemo-content">
          <ol class="breadcrumb">
            <li><a href="<?=site_url()?>/product/index">商品管理</a></li>
            <li class="active">添加商品</li>
          </ol>
          <div class="row margin-bottom-30">
            <div class="col-md-12">
              <ul class="nav nav-pills">
                <li class="active"><a href="<?=site_url()?>/product/index">返回商品列表</a></li>
              </ul>          
            </div>
          </div> 
          <div class="row">
            <div class="col-md-12">
              <div class="table-responsive">
                <h4 class="margin-bottom-15">添加商品</h4>
                 <form role="form" id="templatemo-preferences-form" onsubmit="return false;">
                 	<div class="row">
                 	<div class="col-md-6 margin-bottom-15">
	              	<label for="firstName" class="control-label">商品图片</label>
	              	<input class="file-" type="file"  id="img" name="Filedata"/>
                	<input id="pic" name="pic" type="hidden" class="input" value="">
                	<img id="img_product" src="1.pic" width="50px;"  height="50px;"/>
                	 </div>
                	 </div>
                  <div class="col-md-6 margin-bottom-15">
                    <label for="firstName" class="control-label">商品名称</label>
                    <input type="text" class="form-control" id="name" value="">                  
                  </div>
                  <div class="col-md-6 margin-bottom-15">
                    <label for="lastName" class="control-label">大类名称</label>
                    <select class="form-control margin-bottom-15" id="category">
                		<option value="0">请选择大类</option>
                  </select>     
                  </div>
                    <div class="col-md-6 margin-bottom-15">
                    <label for="firstName" class="control-label">小类名称</label>
                    <select class="form-control margin-bottom-15" id="variety">
                   		<option value="0">请选择小类</option>
                  </select>    
                  </div>
                  <div class="col-md-6 margin-bottom-15">
                    <label for="firstName" class="control-label">进货价</label>
                    <input type="text" class="form-control" id="purprice" value="">                 
                  </div>
                    <div class="col-md-6 margin-bottom-15">
                    <label for="lastName" class="control-label">出售价</label>
                    <input type="text" class="form-control" id="price" value="">                 
                  </div>
	              <div class="col-md-6 margin-bottom-15">
                    <label for="firstName" class="control-label">商品单位</label>
                    <input type="text" class="form-control" id="unit" value="">                 
                  </div>
                  <div class="col-md-6 margin-bottom-15">
                    <label for="lastName" class="control-label">商品备注</label>
                    <input type="text" class="form-control" id="description" value="">                 
                  </div>
                   <div class="col-md-6 margin-bottom-15">
                    <label for="firstName" class="control-label">排序号</label>
                    <input type="text" class="form-control" id="position" value="">                 
                  </div>
                   <div class="col-md-6 margin-bottom-15">
                    <label for="lastName" class="control-label">库存</label>
                    <input type="text" class="form-control" id="stock" value="">                 
                  </div>
                    <div class="col-md-6 margin-bottom-15">
                    <label for="firstName" class="control-label">特价商品</label>
                    <select class="form-control margin-bottom-15" id="sale">
                   		<option value="0">否</option>
                   		<option value="1">是</option>
                  </select>    
                  </div>
                  <div class="col-md-6 margin-bottom-15">
                    <label for="lastName" class="control-label">类型</label>
                    <select class="form-control margin-bottom-15" id="type">
                   		<option value="0">普通商品</option>
                   		<option value="1">食堂商品</option>
                   		<option value="2">特供商品</option>
                   		<option value="3">乔司鱼府特供商品</option>
                  </select>    
                  </div>
	              <div class="row templatemo-form-buttons">
	                <div class="col-md-12">
	                  <button type="submit" class="btn btn-primary" id="submit_btn">添加</button>
	                </div>
                </div>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
<script>
	$(document).ready(function() {
		$('#img').ajaxfileupload({
				'action' : '<?php echo site_url('up/up_pic')?>',
				'params' : {
					'path' : 'product/pic'
				},
				onStart: function() {
					
				},
				'onComplete' : function(response) {
					if ( typeof response === 'string') {
						var files = response.replace(/\\/g, '/');
						$("#img_product").attr("src",'http://7xj0kd.com2.z0.glb.qiniucdn.com/'+files);
						$("#pic").val(files);
						alert("上传成功！");
					} else {
						alert("上传的文件格式不正确");
					}
				}
			});
		
		$.ajax({
             type: "post",
             url: "<?php echo site_url('product/getcategory')?>", 
             data: {"categoryid": "0"},
             dataType: "json",
             timeout : "20000",
             error : function() {
				alert("网络连接超时，请稍后再试");
			 },
             success: function(data) {
                 $("#category").html("<option value=''>请选择大类</option>");
                 $.each(data.data, function(i, item) {
                     $("#category").append("<option value='" + item.id + "'>" + item.name + "</option>");
                 });
             }
         });

		  $("#category").change(function() {
              $.ajax({
                  type: "post",
                  url: "<?php echo site_url('product/getcategory')?>", 
                  data: {"categoryid": $(this).val()},
                  dataType: "json",
                  timeout : "20000",
                  error : function() {
  					alert("网络连接超时，请稍后再试");
  				  },
                  success: function(data) {
                      $("#variety").html("<option value=''>请选择小类</option>");
                      $.each(data.data, function(i, item) {
                          $("#variety").append("<option value='" + item.id + "'>" + item.name + "</option>");
                      });
                  }
              });
          });
          
		$("#submit_btn").click(function() {
			var name = $("#name").val();
			var purprice = $("#purprice").val();
			var price = $("#price").val();
			var image = $("#pic").val();
			var unit = $("#unit").val();
			var description = $("#description").val();
			var position = $("#position").val();
			var categoryid = $("#category").val();
			var varietyid = $("#variety").val();
			if(categoryid==0){
				alert("请选择大类！");
				return;
			}
			if(varietyid==0){
				alert("请选择小类！");
				return;
			}
			var stock = $("#stock").val();
			var sale = $("#sale").val();
			var type = $("#type").val();
			$.ajax({
				url : '<?php echo site_url('product/save')?>',
				type : "post",
				async : false,
				data : {
					'name' : name,
					'purprice' : purprice,
					'price' : price,
					'image' : image,
					'unit' : unit,
					'description' : description,
					'position' : position,
					'categoryid' : categoryid,
					'varietyid' : varietyid,
					'stock' : stock,
					'sale' : sale,
					'type' : type
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
					
				}
			});
		});
	}); 
</script>